import 'package:flutter/gestures.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:hzy_normal_network/hzy_normal_network.dart';

import '../../tools/index.dart';
import 'http_normal_config.dart';

// 启动配置
class LaunchConfig {
  static init() async {
    WidgetsFlutterBinding.ensureInitialized();

    //滚动性能优化 1.22.0
    GestureBinding.instance.resamplingEnabled = false;
    await initThirdParty();
    await initNetWork();
    await initNormalDefault();
  }

  /// 初始化第三发
  static initThirdParty() async {
    // 初始化shared_preferences
    await SharedPreferencesTool.getInstance();

    // 配置EasyLoading
    EasyLoading.instance.displayDuration = const Duration(milliseconds: 1500);

    /// 配置网络缓存
    await DioCacheTool.initCacheStore();
  }

  /// 配置默认数据
  static initNormalDefault() async {}

  /// 配置网络请求
  static initNetWork() async {
    /**
     * 后台返回数据格式
     * {
     *  code:0,
     *  msg:"成功",
     *  data:{},
     * }
     * 
    */
    HzyNormalDefaultTransFormer.getInstance().setHttpConfig(
      config: HzyNormalHttpResponeConfig(
        status: 'code', // 状态码字段 默认code
        msg: "msg", // 返回信息字段 默认msg
        data: "data", // 返回数据字段 默认data
        successCode: 0, // 成功状态码 默认0
      ),
    );
    // 注入网络请求组件
    NetWorkInstance().configClient(
      config: HzyNormalHttpConfig(
        baseUrl: HttpNormalConfig.configbaseUrl(),
        interceptors: [
          /// 拦截器
          /// 网络缓存拦截器
          DioCacheTool.init()
        ],
        isNeedLog: true,
      ),
    );
  }
}
